#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>


pthread_mutex_t beers_lock = PTHREAD_MUTEX_INITIALIZER;

int beers = 2000000;

void *drink_lots()
{
	int i;
	pthread_mutex_lock(&beers_lock);
	for (i = 0; i < 100000; i++) {
		beers--;
	}
	pthread_mutex_unlock(&beers_lock);
	printf("beers = %i\n", beers);
	return NULL;
}

void *drink_another()
{
	int i;
	for (i = 0; i < 100000; i++) {
		pthread_mutex_lock(&beers_lock);
		beers--;
		pthread_mutex_unlock(&beers_lock);
	}
	printf("beers = %i\n", beers);
	return NULL;
}

int main(int argc, char *args[])
{
	pthread_t threads[20];
	int i = 0;
	for (i = 0; i < 20; i++)
		// pthread_create(&threads[i], NULL, drink_lots, NULL);
		pthread_create(&threads[i], NULL, drink_another, NULL);	
	void *result;
	for (i = 0; i < 20; i++)
		pthread_join(threads[i], &result);
	printf("Left: %i\n", beers);
	puts("Thread run finished");
}
